package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.Role;
import com.hotelManagerSystem.model.RoomOrder;
import com.hotelManagerSystem.model.Staff;
import com.hotelManagerSystem.service.IRoleService;
import com.hotelManagerSystem.service.IStaffRoleService;
import com.hotelManagerSystem.service.IStaffService;
import com.hotelManagerSystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  角色前端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IStaffRoleService iStaffRoleService;

    //    角色列表
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                       @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                       @RequestParam(required = false,value="roleName")String roleName
    ){
//        System.out.println("------------list---------------");
        PageUtil.runStartPage(pageNum,pageSize);
        List<Role> role_dbList ;
        if(roleName == null){
            role_dbList = iRoleService.queryRoleList();
        }else {
            role_dbList = iRoleService.queryRoleList();
        }
//        分页对象
        PageInfo pageInfo = PageUtil.getPageInfo(role_dbList);
//        装填model
        model.addAttribute("role_dbLis",role_dbList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("roleName",roleName);
//        System.out.println(role_dbList);
        return "admin/rolePage/roleList";
    }

    //    角色添加页面跳转
    @RequestMapping("/addUI")
    public String addUI(Model model){
//        System.out.println("------------addUI---------------");

        return "admin/rolePage/addRole";
    }

    //    角色添加
//    @RequestMapping("add")
//    public String add(Model model,Role role){
//        System.out.println("------------add---------------");
////        System.out.println(role);
//
//        boolean b = iRoleService.save(role);
//
////        跳转回角色列表页面
////        return list(model);
//
//        List<Role> role_dbList = iRoleService.queryRoleList();
//        model.addAttribute("role_dbList",role_dbList);
//
//        return "admin/index";
//    }

    /**
     *添加一个角色
     * @author longyajr
     * @param role
     * @return
     */

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Model model, Role role){
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoleService.save(role);
        if(b){
            map.put("code",200);
        }else{
            map.put("code",500);
        }
        return map;
    }

    //    角色删除
//    @RequestMapping("delete")
//    public String delete(Model model,String roleIdStr){
////        System.out.println("------------delete---------------");
////        System.out.println(roleIdStr);
//        int roleId = Integer.parseInt(roleIdStr);
//        boolean b = iRoleService.removeById(roleId);
//
////        跳转回角色列表页面
////        return list(model);
//
//        List<Role> role_dbList = iRoleService.queryRoleList();
//        model.addAttribute("role_dbList",role_dbList);
//
//        return "admin/index";
//    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(int roleId){
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoleService .removeById(roleId);
        boolean c=iStaffRoleService.deleteStaffRoleByRoleId(roleId);
        if(b&&c){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }

    //    角色编辑页面跳转
    @RequestMapping("updateUI")
    public String updateUI(Model model,int roleId){
//        System.out.println("------------updateUI---------------");
        Role role_db = iRoleService.getById(roleId);
        model.addAttribute("role_db",role_db);
        return "admin/rolePage/updateRole";
    }

    //    角色编辑
//    @RequestMapping("update")
//    public String update(Model model,Role role){
////        System.out.println("------------update---------------");
////        System.out.println(role);
//
//        boolean b = iRoleService.saveOrUpdate(role);
//
////        跳转回角色列表页面
////        return list(model);
//
//        List<Role> role_dbList = iRoleService.queryRoleList();
//        model.addAttribute("role_dbList",role_dbList);
//
//        return list(model,1,8,"");
//    }
    /**
     * 修改一个角色
     * @author longyajr
     * @param role
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(Role role){
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoleService.saveOrUpdate(role);
        if(b){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }
}

